package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.FoodSpec;
import com.ruoyi.system.domain.vo.FoodDataVo;
import com.ruoyi.system.domain.vo.SpecParentVo;
import com.ruoyi.system.domain.vo.SpecVo;
import com.ruoyi.system.mapper.FoodSpecMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FoodMapper;
import com.ruoyi.system.domain.Food;
import com.ruoyi.system.service.IFoodService;

/**
 * 菜品信息Service业务层处理
 *
 * @author Wxl
 * @date 2022-03-16
 */
@Service
public class FoodServiceImpl implements IFoodService {
    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private SysDictDataMapper dictMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private FoodSpecMapper foodSpecMapper;

    /**
     * 查询菜品信息
     *
     * @param id 菜品信息主键
     * @return 菜品信息
     */
    @Override
    public Food selectFoodById(Long id) {
        return foodMapper.selectFoodById(id);
    }

    /**
     * 查询菜品信息列表
     *
     * @param food 菜品信息
     * @return 菜品信息
     */
    @Override
    public List<Food> selectFoodList(Food food) {
        return foodMapper.selectFoodList(food);
    }

    /**
     * 新增菜品信息
     *
     * @param food 菜品信息
     * @return 结果
     */
    @Override
    public int insertFood(Food food) {
        long id = new Date().getTime();
        food.setId(id);
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictLabel(food.getFoodName());
        sysDictData.setDictValue(String.valueOf(id));
        sysDictData.setDictSort(999L);
        sysDictData.setDictType("food_id");
        sysDictData.setCssClass("1");
        sysDictData.setListClass("success");
        sysDictData.setStatus("0");
        sysDictData.setIsDefault("N");
        sysDictData.setCreateBy("admin");
        dictMapper.insertDictData(sysDictData);

        food.setCreateTime(DateUtils.getNowDate());
        return foodMapper.insertFood(food);
    }

    /**
     * 修改菜品信息
     *
     * @param food 菜品信息
     * @return 结果
     */
    @Override
    public int updateFood(Food food) {
        return foodMapper.updateFood(food);
    }

    /**
     * 批量删除菜品信息
     *
     * @param ids 需要删除的菜品信息主键
     * @return 结果
     */
    @Override
    public int deleteFoodByIds(Long[] ids) {
        return foodMapper.deleteFoodByIds(ids);
    }

    /**
     * 删除菜品信息信息
     *
     * @param id 菜品信息主键
     * @return 结果
     */
    @Override
    public int deleteFoodById(Long id) {
        return foodMapper.deleteFoodById(id);
    }

    @Override
    public List<FoodDataVo> getFoodDataVo(Long shopId) {
        List<FoodDataVo> foodDataVoList = new ArrayList<>();
        List<Food> foodList = foodMapper.selectList(new QueryWrapper<Food>().eq("shop_id", shopId));
        for (Food food : foodList) {
            FoodDataVo foodDataVo = new FoodDataVo();
            foodDataVo.setId(food.getId());
            foodDataVo.setName(food.getFoodName());
            foodDataVo.setCategoryId(food.getCategoryId());
            foodDataVo.setDescripe(food.getFoodDesc());
            foodDataVo.setImg("http://localhost:8000/dev-api"+food.getFoodCover());
            foodDataVo.setPrice(food.getSellPrice());
            foodDataVo.setOldprice(food.getOriginPrice());
            SpecParentVo specParentVo = new SpecParentVo();
            List<SpecVo> speclist = new ArrayList<>();
            if (!StringUtils.isEmpty(food.getFoodSpecId())) {

                SysDictData food_spec_parent = sysDictDataMapper.selectOne(new QueryWrapper<SysDictData>()
                        .eq("dict_value", food.getFoodSpecId())
                        . eq("dict_type", "food_spec_parent"));
                specParentVo = new SpecParentVo();
                specParentVo.setId(Long.valueOf(food_spec_parent.getDictValue()));
                specParentVo.setName(food_spec_parent.getDictLabel());
                foodDataVo.setForm(specParentVo);
                List<FoodSpec> food_id = foodSpecMapper.selectList(new QueryWrapper<FoodSpec>().eq("food_id", food.getId()));
                food_id.forEach(item -> {
                    SpecVo specVo = new SpecVo();
                    specVo.setId(item.getId());
                    specVo.setName(item.getName());
                    specVo.setPrice(item.getPrice());
                    specVo.setOldprice(item.getOldPrice());
                    specVo.setSelect(false);
                    specVo.setPid(Long.valueOf(food_spec_parent.getDictValue()));
                    speclist.add(specVo);
                });

                foodDataVo.setForm_child(speclist);
            }
            foodDataVoList.add(foodDataVo);
        }

        return foodDataVoList;
    }
}
